package es.ats.codington.festival.daos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;





import es.ats.codington.festival.domain.LargeBusiness;
import es.ats.codington.festival.domain.Museum;
import es.ats.codington.festival.domain.Park;
import es.ats.codington.festival.domain.Places;
import es.ats.codington.festival.domain.Stadium;
import es.ats.codington.festival.domain.Theater;
import es.ats.codington.festival.domain.TouristAttractions;
import es.ats.codington.festival.domain.TraditionalMarkets;
import es.ats.codington.festival.domain.Zoo;
import es.ats.codington.festival.interfaces.daos.IPlaceDAOImp;




public class PlaceDAOImp implements IPlaceDAOImp{
	
	
	public PlaceDAOImp(){
		
	}
	
	public Places getPlaceById(int placeId, Connection con){
		
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		Places place;
		
		try{		
			statement = con.prepareStatement("select * from place where placeid = ?");
			statement.setInt(1, placeId);
			resultSet = statement.executeQuery();
			resultSet.next();
				
			switch (resultSet.getInt("placetype")) {
				case LargeBusiness.placeType:
					place = new LargeBusiness(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					break;
					
				case Museum.placeType:
					place = new Museum(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					break;

				case Park.placeType:
					place = new Park(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					break;
					
				case Stadium.placeType:
					place = new Stadium(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					break;
					
				case Theater.placeType:
					place = new Theater(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					break;
					
				case TouristAttractions.placeType:
					place = new TouristAttractions(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					break;
					
				case TraditionalMarkets.placeType:
					place = new TraditionalMarkets(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					break;
					
				case Zoo.placeType:
					place = new Zoo(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					break;
				default: 
					place = new Museum();
					break;
			}
				
		}catch (SQLException e){
			System.out.println(e.getMessage());
			place = new Museum();
		}
		
		return place;

	}
	
	public int insertPlace(Places places, Connection con){
		int aRows=0;
		PreparedStatement statement = null;
		try{			
			statement = con.prepareStatement("insert into place(placename,placetype,capacity,placedescription,zone) values(?,?,?,?,?);");
			statement.setString(1,places.getPlaceName());
			statement.setInt(2,places.getPlaceType());
			statement.setInt(3,places.getCapacity());
			statement.setString(4,places.getPlaceDescription());
			statement.setString(5,places.getZone());
			
			con.setAutoCommit(false);
			
			aRows = statement.executeUpdate();
			con.commit();
			
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return aRows;
	}
	
	
	public int deletePlace(int placeId, Connection con){
		int aRows = 0;
		
		try{
			PreparedStatement statement = con.prepareStatement("delete from place where placeid = ?");
			statement.setInt(1, placeId);
			
			con.setAutoCommit(false);
			
			aRows = statement.executeUpdate();
			con.commit();
			
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		
		return aRows;
	}
	
	public int updatePlace(Places uPlace, Connection con){
		int aRows = 0;
		
		try{
			PreparedStatement statement = con.prepareStatement("update place set placename = ?, capacity = ?, placedescription = ?, zone = ? where placeid=?");
			statement.setString(1,uPlace.getPlaceName());
			statement.setInt(2,uPlace.getCapacity());
			statement.setString(3,uPlace.getPlaceDescription());
			statement.setString(4,uPlace.getZone());
			statement.setInt(5,uPlace.getPlaceId());

			con.setAutoCommit(false);
			
			aRows = statement.executeUpdate();
			con.commit();
			
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		
		return aRows;
	}
	
	public ArrayList<Places> getAllPlaces(Connection con){
		ArrayList<Places> listPlaces = new ArrayList<Places>();
		ResultSet resultSet = null;
		PreparedStatement statement = null;
		
		try{

			statement = con.prepareStatement("select * from place");
			resultSet = statement.executeQuery();
			
			while(resultSet.next()){
				switch (resultSet.getInt("placetype")) {
				case LargeBusiness.placeType:
					LargeBusiness largeBusiness = new LargeBusiness(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					listPlaces.add(largeBusiness);
					break;
				case Museum.placeType:
					Museum museum = new Museum(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					listPlaces.add(museum);
					break;
				case Park.placeType:
					Park park = new Park(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					listPlaces.add(park);
					break;
				case Stadium.placeType:
					Stadium stadium = new Stadium(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					listPlaces.add(stadium);
					break;
				case Theater.placeType:
					Theater theater = new Theater(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					listPlaces.add(theater);
					break;
				case TouristAttractions.placeType:
					TouristAttractions touristAttractions = new TouristAttractions(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					listPlaces.add(touristAttractions);
					break;
				case TraditionalMarkets.placeType:
					TraditionalMarkets traditionalMarkets = new TraditionalMarkets(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					listPlaces.add(traditionalMarkets);
					break;
				case Zoo.placeType:
					Zoo zoo = new Zoo(resultSet.getInt("placeid"),resultSet.getString("placename"),resultSet.getInt("capacity"),resultSet.getString("placedescription"),resultSet.getString("zone"));
					listPlaces.add(zoo);
					break;
				default:
					break;
				}
			}	
			resultSet.close();
			
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
				
		return listPlaces;
	}
}
